<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>单例模式</title>
</head>
<body>
<script>
    var Singleton = function (name) {
        this.name = name;
        this.instance = null;
        console.log(this.instance);
    }
    Singleton.prototype.getName = function () {
        alert(1);
    }
    Singleton.getInstance = function(name){
        console.log(this);
        if(!this.instance){
            return this.instance = new Singleton(name)
        }
        return this.instance;
    }
    console.log(this.instance);
    var a = Singleton.getInstance("sven1");
    var b = Singleton.getInstance("sven2");

 /*   var Singleton = function (name) {
        this.name= name;
    };
    Singleton.prototype.getName = function () {
        alert(this.name);
    }
    Singleton.getInstance = (function () {
        var instance = null;
        return function (name) {
            if(!instance){
                instance = new Singleton(name)
            }
            return instance;
        }
    })()*/
</script>
</body>
</html>